﻿// XML LOADER CLASS

package scripts {
	
	import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
	
	public class XMLLoader extends EventDispatcher {
		
		private static var theXML:XML;
		
		public function XMLLoader(path:String){
			loadXML(path);
		}
		
		private function loadXML(xmlPath:String):void {
			var dataRequest:URLRequest = new URLRequest(xmlPath);
			var loader:URLLoader = new URLLoader(dataRequest);
			loader.addEventListener(Event.COMPLETE, xmlLoaded);
		}
		
		private function xmlLoaded(evt:Event):void {
			 try {
				theXML = new XML(evt.target.data);
				dispatchEvent(new Event(Event.COMPLETE));
   			 } catch (evt:TypeError){
				 trace("ERROR- XML file is not format correctly.");
				 dispatchEvent(new Event(ErrorEvent.ERROR));
				 }
		}
		
		public function getXML():XML {
            return theXML;
        }
		
	}
	
}